import { useState, useEffect } from 'react'
import { useFetch } from '@/plugins/axios'
import { UserInfoProps } from '@/utils/interface'
import { FetchUserInfoProps, transUserInfo } from '@/utils/translator'
import { currentHost } from '@/utils/baseVariable'
/**获取登录的用户信息 */
export const useUser = () => {
	const [userInfo, setUserInfo] = useState<UserInfoProps>({})
	//获取用户信息
	const [{ loading, data }, reFetch] = useFetch<FetchUserInfoProps>(
		{
			// url: '/sas/login?username=sysmgr&password=keda8888',
			// jmsapi/login?username=secmgr&password=keda888888
			url: '/web/sso/session',
			method: 'get',
		},
		{
			cacheKey: 'userInfo',
			cacheTime: Infinity,
		}
	)
	useEffect(() => {
		if (!loading) {
			if (data) {
				let _data = transUserInfo(data)
				if (!_data.sacAdmin) {
					// window.location.href = currentHost + '/portal/home'
				}
				setUserInfo(_data)
			}
		}
	}, [data, loading])

	return { userInfo, reFetch }
}
